﻿using Model;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tools;
using 纺织贸易管理系统;

namespace BLL
{
    public class 生产入库单BLL
    {
        /// <summary>
        /// 删除单据
        /// </summary>
        /// <param name="danhao">要删除的单号</param>
        public static void Delete(string danhao)
        {
            单据反审核(danhao);
            var danju = Connect.dbcontext.Queryable<DanjuTable>().First(x => x.dh == danhao);
            Connect.dbcontext.Deleteable<DanjuTable>().Where(x => x.dh == danhao).ExecuteCommand();
            Connect.dbcontext.Deleteable<danjumingxitable>().Where(x => x.danhao == danhao).ExecuteCommand();
            foreach (var s in Connect.dbcontext.Queryable<StockTable>().Where(x => x.RKDH == danhao).ToList())
            {
                Connect.dbcontext.Deleteable<JuanHaoTable>().Where(x => x.Ckmc == s.ID.ToString()).ExecuteCommand();
            }
            Connect.dbcontext.Deleteable<StockTable>().Where(x => x.RKDH == danhao).ExecuteCommand();
            if (danju != null)
            {
                Connect.dbcontext.Insertable<DeleteLog>(new DeleteLog() { date = DateTime.Now, Reason = "删除入库单", useID = CurrentUser.user.YHMC, Log = "删除入库单" +JsonConvert.SerializeObject(danju) }).ExecuteCommand();
            }
        }
        /// <summary>
        /// 保存单据
        /// </summary>
        /// <param name="danju"></param>
        /// <param name="danjumingxitables"></param>
        public static void 保存(DanjuTable danju, List<danjumingxitable> danjumingxitables)
        {

            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据审核(danju, danjumingxitables);
                }
            }
            else
            {
                单据审核(danju, danjumingxitables);
            }
        }
        /// <summary>
        /// 审核单据
        /// </summary>
        /// <param name="danhao"></param>
        public static void 单据审核(DanjuTable danju, List<danjumingxitable> danjumingxitables)
        {

            //库存BLL.增加库存(danjumingxitables, danju);
            List<StockTable> stocks = new List<StockTable>();
            foreach (var mingxi in danjumingxitables)
            {
                StockTable stock = Connect.dbcontext.Queryable<StockTable>().First(x => x.RKDH == danju.dh && x.PM == mingxi.pingming && x.GG == mingxi.guige
               && x.GH == mingxi.ganghao && x.YS == mingxi.yanse && x.ColorNum == mingxi.ColorNum && x.MF == mingxi.menfu && x.KZ == mingxi.kezhong
               && x.kuanhao == mingxi.kuanhao && x.Huahao == mingxi.Huahao && x.orderNum == mingxi.OrderNum);
                if (stock == null)
                {
                    stock = new StockTable();
                    stock.AvgPrice = mingxi.hanshuidanjia + mingxi.AveragePrice;
                    stock.BH = mingxi.Bianhao;
                    stock.biaoqianmishu = 0;
                    stock.CF = mingxi.chengfeng;
                    stock.CKMC = danju.ckmc;
                    stock.ContractNum = mingxi.ContractNum;
                    stock.CustomName = mingxi.CustomName;
                    stock.GG = mingxi.guige;
                    stock.GH = mingxi.ganghao;
                    stock.houzhengli = mingxi.houzhengli;
                    stock.JS = mingxi.chengpingjuanshu;
                    stock.kaijianmishu = 0;
                    stock.kuanhao = mingxi.kuanhao;
                    stock.Kuwei = mingxi.Kuwei;
                    stock.KZ = mingxi.kezhong;
                    stock.MF = mingxi.menfu;
                    stock.MS = mingxi.chengpingmishu;
                    stock.orderNum = mingxi.OrderNum;
                    stock.own = danju.own;
                    stock.PM = mingxi.pingming;
                    stock.RKDH = mingxi.danhao;
                    stock.RQ = danju.rq;
                    stock.TotalMoney = mingxi.hanshuiheji;
                    stock.YS = mingxi.yanse;
                    stock.Huahao = mingxi.Huahao;
                    stock.ColorNum = mingxi.ColorNum;
                    stock.CustomerColorNum = mingxi.CustomerColorNum;
                    stock.CustomerPingMing = mingxi.CustomerPingMing;
                    stock.Rangchang = mingxi.Rangchang;
                    stock.RukuNum = mingxi.chengpingmishu;
                    stock.YiFaNum = 0;
                    stock.Difference = mingxi.chengpingmishu;
                    stock.LiuzhuanCard = mingxi.offerid;
                    stock.FabricStyle = mingxi.FabricStyle;
                    stocks.Add(stock);
                }
                else
                {
                    stock.AvgPrice = mingxi.hanshuidanjia + mingxi.AveragePrice;
                    stock.BH = mingxi.Bianhao;
                    stock.biaoqianmishu = 0;
                    stock.CF = mingxi.chengfeng;
                    stock.CKMC = danju.ckmc;
                    stock.ContractNum = mingxi.ContractNum;
                    stock.CustomName = mingxi.CustomName;
                    stock.GG = mingxi.guige;
                    stock.GH = mingxi.ganghao;
                    stock.houzhengli = mingxi.houzhengli;
                    stock.JS = mingxi.chengpingjuanshu;
                    stock.kaijianmishu = 0;
                    stock.kuanhao = mingxi.kuanhao;
                    stock.Kuwei = mingxi.Kuwei;
                    stock.KZ = mingxi.kezhong;
                    stock.MF = mingxi.menfu;
                    stock.MS = mingxi.chengpingmishu;
                    stock.orderNum = mingxi.OrderNum;
                    stock.own = danju.own;
                    stock.PM = mingxi.pingming;
                    stock.RKDH = mingxi.danhao;
                    stock.RQ = danju.rq;
                    stock.TotalMoney = mingxi.hanshuiheji;
                    stock.YS = mingxi.yanse;
                    stock.Huahao = mingxi.Huahao;
                    stock.ColorNum = mingxi.ColorNum;
                    stock.CustomerColorNum = mingxi.CustomerColorNum;
                    stock.CustomerPingMing = mingxi.CustomerPingMing;
                    stock.Rangchang = mingxi.Rangchang;
                    stock.RukuNum = mingxi.chengpingmishu;
                    stock.YiFaNum = stock.YiFaNum;
                    stock.Difference = mingxi.chengpingmishu;
                    stock.FabricStyle = mingxi.FabricStyle;
                    Connect.dbcontext.Updateable<StockTable>(stock).ExecuteCommand();
                }
            }
            Connect.dbcontext.Insertable(stocks).ExecuteCommand();
            //架子入库
            架子BLL.架子入库(danjumingxitables);
            架子BLL.进仓日志(danjumingxitables, danju);
            单据BLL.审核(danju.dh);
        }
        public static void 修改单据审核(DanjuTable danju, List<danjumingxitable> danjumingxitables)
        {
            //架子出库
            架子BLL.架子出库(danjumingxitables);
            架子BLL.删除日志(danju.dh);

            //库存BLL.增加库存(danjumingxitables, danju);
            List<StockTable> stocks = new List<StockTable>();
            foreach (var mingxi in danjumingxitables)
            {
                StockTable stock = Connect.dbcontext.Queryable<StockTable>().First(x => x.RKDH == danju.dh && x.PM == mingxi.pingming && x.GG == mingxi.guige
               && x.GH == mingxi.ganghao && x.YS == mingxi.yanse && x.ColorNum == mingxi.ColorNum && x.MF == mingxi.menfu && x.KZ == mingxi.kezhong
               && x.kuanhao == mingxi.kuanhao && x.Huahao == mingxi.Huahao && x.orderNum == mingxi.OrderNum);
                if (stock != null)
                {
                    stock.AvgPrice = mingxi.hanshuidanjia + mingxi.AveragePrice;
                    stock.BH = mingxi.Bianhao;
                    stock.biaoqianmishu = 0;
                    stock.CF = mingxi.chengfeng;
                    stock.CKMC = danju.ckmc;
                    stock.ContractNum = mingxi.ContractNum;
                    stock.CustomName = mingxi.CustomName;
                    stock.GG = mingxi.guige;
                    stock.GH = mingxi.ganghao;
                    stock.houzhengli = mingxi.houzhengli;
                    stock.JS = mingxi.chengpingjuanshu;
                    stock.kaijianmishu = 0;
                    stock.kuanhao = mingxi.kuanhao;
                    stock.Kuwei = mingxi.Kuwei;
                    stock.KZ = mingxi.kezhong;
                    stock.MF = mingxi.menfu;
                    stock.MS = mingxi.chengpingmishu;
                    stock.orderNum = mingxi.OrderNum;
                    stock.own = danju.own;
                    stock.PM = mingxi.pingming;
                    stock.RKDH = mingxi.danhao;
                    stock.RQ = danju.rq;
                    stock.TotalMoney = mingxi.hanshuiheji;
                    stock.YS = mingxi.yanse;
                    stock.Huahao = mingxi.Huahao;
                    stock.ColorNum = mingxi.ColorNum;
                    stock.CustomerColorNum = mingxi.CustomerColorNum;
                    stock.CustomerPingMing = mingxi.CustomerPingMing;
                    stock.Rangchang = mingxi.Rangchang;
                    stock.RukuNum = mingxi.chengpingmishu;
                    stock.YiFaNum = stock.YiFaNum;
                    stock.Difference = mingxi.chengpingmishu;
                    stock.LiuzhuanCard = mingxi.offerid;
                    Connect.dbcontext.Updateable<StockTable>(stock).ExecuteCommand();
                }
                else
                {
                    //发过货的入库单不允许修改库存数量
                    if (Connect.dbcontext.Queryable<danjumingxitable,DanjuTable>((x,y)=>x.danhao==y.dh&&y.djlx==DanjuLeiXing.销售出库单).First(x => x.rkdh == mingxi.rkdh && x.ganghao == mingxi.ganghao && x.pingming==mingxi.pingming
                    &&x.yanse==mingxi.yanse) == null)
                    {
                        stock = new StockTable
                        {
                            AvgPrice = mingxi.hanshuidanjia + mingxi.AveragePrice,
                            BH = mingxi.Bianhao,
                            biaoqianmishu = 0,
                            CF = mingxi.chengfeng,
                            CKMC = danju.ckmc,
                            ContractNum = mingxi.ContractNum,
                            CustomName = mingxi.CustomName,
                            GG = mingxi.guige,
                            GH = mingxi.ganghao,
                            houzhengli = mingxi.houzhengli,
                            JS = mingxi.chengpingjuanshu,
                            kaijianmishu = 0,
                            kuanhao = mingxi.kuanhao,
                            Kuwei = mingxi.Kuwei,
                            KZ = mingxi.kezhong,
                            MF = mingxi.menfu,
                            MS = mingxi.chengpingmishu,
                            orderNum = mingxi.OrderNum,
                            own = danju.own,
                            PM = mingxi.pingming,
                            RKDH = mingxi.danhao,
                            RQ = danju.rq,
                            TotalMoney = mingxi.hanshuiheji,
                            YS = mingxi.yanse,
                            Huahao = mingxi.Huahao,
                            ColorNum = mingxi.ColorNum,
                            CustomerColorNum = mingxi.CustomerColorNum,
                            CustomerPingMing = mingxi.CustomerPingMing,
                            Rangchang = mingxi.Rangchang,
                            RukuNum = mingxi.chengpingmishu,
                            YiFaNum = 0,
                            Difference = mingxi.chengpingmishu,
                            LiuzhuanCard=mingxi.offerid
                        };
                        stocks.Add(stock);
                    }
                }
            }
            Connect.dbcontext.Insertable(stocks).ExecuteCommand();
            //架子入库
            架子BLL.架子入库(danjumingxitables);
            架子BLL.进仓日志(danjumingxitables, danju);
            单据BLL.审核(danju.dh);
        }
        /// <summary>
        /// 单据反审核
        /// </summary>
        /// <param name="danhao"></param>
        public static void 单据反审核(string danhao)
        {
            var danju = DanjuTableService.GetOneDanjuTable(x => x.dh == danhao);
            var danjumingxitables = danjumingxitableService.Getdanjumingxitablelst(x => x.danhao == danhao&&x.toupimishu ==0);
            减少入库数量(danju, danjumingxitables);
            //架子出库
            var jiazilist = new List<JiaziTable>();
            foreach (string jiazi in danjumingxitables.Where(x => !string.IsNullOrWhiteSpace(x.Kuwei)).Select(x => x.Kuwei).Distinct().ToList())
            {
                Connect.dbcontext.Deleteable<JiaziTable>().Where(x => x.JiaziID == jiazi).ExecuteCommand();
            }
            单据BLL.未审核(danhao);
        }
        /// <summary>
        /// 修改单据
        /// </summary>
        /// <param name="danju"></param>
        /// <param name="danjumingxitables"></param>
        public static void 修改单据(DanjuTable danju, List<danjumingxitable> danjumingxitables)
        {
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    单据反审核(danju.dh);
                }
            }
            else
            {
                单据反审核(danju.dh);
            }
            Connect.dbcontext.Updateable(danju).ExecuteCommand();
            DeleteLogService.InsertDeleteLog(new DeleteLog() { date = DateTime.Now, Log = $"修改入库单号{danju.dh} 内容是" + JsonConvert.SerializeObject(danju), useID = Tools.CurrentUser.user.YHBH });
            Connect.dbcontext.Deleteable<danjumingxitable>().Where(x => x.danhao == danju.dh&&x.toupimishu ==0).ExecuteCommand();
            var mingxi = danjumingxitables.Where(x => !string.IsNullOrWhiteSpace(x.pingming)).ToList();
            foreach (var m in mingxi)
            {
                m.danhao = danju.dh;
                m.CustomName = danju.ksmc; 
            }
           Connect.dbcontext.Insertable(mingxi).ExecuteCommand();
            if (SysInfo.GetInfo.own != string.Empty)
            {
                if (SysInfo.GetInfo.own != "审核制")
                {
                    修改单据审核(danju, mingxi);
                }
            }

            //库存BLL.清零库存();
        }
        private static void 减少入库数量(DanjuTable danjuTable, List<danjumingxitable> danjumingxitables)
        { //StockTableService.UpdateStockTable
            Connect.dbcontext.Deleteable<StockTable>().Where(x => x.RKDH == danjuTable.dh&&x.YiFaNum==0).ExecuteCommand();
        }
    }
}
